package com.zrrd.network.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author WanJl
 * @version 1.0
 * @title SendDemo03
 * @description 广播发送
 * @create 2023/10/22 11:29
 */
public class SendDemo03 {
    public static void send(String host,int port, String content) throws IOException {
        //1. 创建发送端Socket对象(DatagramSocket)
        DatagramSocket ds=new DatagramSocket();
        //2. 创建存储数据的箱子,将广播地址封装进去
        byte[] bytes = content.getBytes();
        DatagramPacket dp=new DatagramPacket(bytes,bytes.length
                ,InetAddress.getByName(host),port);
        //3. 发送数据
        ds.send(dp);
        //4. 释放资源
        ds.close();
    }

    public static void main(String[] args) throws IOException {
        send("255.255.255.255",10011,"广播信息啦啦");
    }
}
